//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package common;

import java.io.UnsupportedEncodingException;

public class ByteStream extends ChecksumEncoder {
  private byte[] streamBuffer;
  private int workIndex;
  private int readEndIndex;
  private int bufferSize;
  private int booleanWorkIndex;

  public ByteStream(int size) {
    this.streamBuffer = new byte[size];
    this.workIndex = 0;
    this.readEndIndex = 0;
    this.bufferSize = size;
  }

  public ByteStream(byte[] stream) {
    this.streamBuffer = stream;
    this.workIndex = 0;
    this.readEndIndex = stream.length;
    this.bufferSize = stream.length;
  }

  public int getWorkIndex() {
    return this.workIndex;
  }

  public int getLength() {
    return this.readEndIndex > this.workIndex?this.readEndIndex:this.workIndex;
  }

  public boolean isAtEnd() {
    return this.workIndex >= this.readEndIndex;
  }

  public void resetOffset() {
    this.workIndex = 0;
  }

  private void ensureCapacity(int size) {
    if(this.workIndex + size > this.bufferSize) {
      this.bufferSize += 100;
      byte[] newBuffer = new byte[this.bufferSize];
      System.arraycopy(this.streamBuffer, 0, newBuffer, 0, this.workIndex);
      this.streamBuffer = newBuffer;
    }

  }

  public int readInt() {
    this.booleanWorkIndex = 0;
    int idx = this.workIndex;
    this.workIndex += 4;
    return (this.streamBuffer[idx + 0] << 24 & -16777216) + (this.streamBuffer[idx + 1] << 16 & 16711680) + (this.streamBuffer[idx + 2] << 8 & '\uff00') + (this.streamBuffer[idx + 3] << 0 & 255);
  }

  public int readShort() {
    this.booleanWorkIndex = 0;
    int idx = this.workIndex;
    this.workIndex += 2;
    return (this.streamBuffer[idx + 0] << 8 & '\uff00') + (this.streamBuffer[idx + 1] << 0 & 255);
  }

  public void skip(int len) {
    this.booleanWorkIndex = 0;
    this.workIndex += len;
  }

  public byte readByte() {
    this.booleanWorkIndex = 0;
    int idx = this.workIndex++;
    return this.streamBuffer[idx];
  }

  public LogicLong readLong() {
    LogicLong ll = new LogicLong();
    ll.decode(this);
    return ll;
  }

  public String readString() {
    int strLen = this.readInt();
    if(strLen <= 0) {
      return strLen < 0?null:new String();
    } else {
      try {
        int e = this.workIndex;
        this.workIndex += strLen;
        return new String(this.streamBuffer, e, strLen, "UTF-8");
      } catch (UnsupportedEncodingException var3) {
        return new String();
      }
    }
  }

  public String readStringReference() {
    int strLen = this.readInt();
    if(strLen <= 0) {
      return new String();
    } else {
      try {
        int e = this.workIndex;
        this.workIndex += strLen;
        return new String(this.streamBuffer, e, strLen, "UTF-8");
      } catch (UnsupportedEncodingException var3) {
        return new String();
      }
    }
  }

  public byte[] readBytes(int bytesLen) {
    if(bytesLen > 0) {
      byte[] rval = new byte[bytesLen];
      int rIndex = this.workIndex;
      this.workIndex += bytesLen;
      System.arraycopy(this.streamBuffer, rIndex, rval, 0, bytesLen);
      return rval;
    } else {
      return new byte[0];
    }
  }

  public byte[] readBytes() {
    int bytesLen = this.readInt();
    if(bytesLen > 0) {
      byte[] rval = new byte[bytesLen];
      int rIndex = this.workIndex;
      this.workIndex += bytesLen;
      System.arraycopy(this.streamBuffer, rIndex, rval, 0, bytesLen);
      return rval;
    } else {
      return new byte[0];
    }
  }

  public boolean readBoolean() {
    int bidx = this.booleanWorkIndex;
    this.workIndex += 8 - this.booleanWorkIndex >> 3;
    ++this.booleanWorkIndex;
    this.booleanWorkIndex &= 7;
    return (1 << bidx & this.streamBuffer[this.workIndex - 1]) != 0;
  }

  public String dumpDataAt(int offset, int bytesToRead) {
    return StringUtils.dumpBytes(this.streamBuffer, offset, bytesToRead);
  }

  public String dumpUnreadData() {
    return this.workIndex < this.readEndIndex?StringUtils.dumpBytes(this.streamBuffer, this.workIndex, this.readEndIndex - this.workIndex):(this.workIndex == this.readEndIndex?"EOF":"Write Stream!");
  }

  private void writeIntToByteArray(int intValue) {
    this.booleanWorkIndex = 0;
    this.ensureCapacity(4);
    this.streamBuffer[this.workIndex + 0] = (byte)(intValue >> 24);
    this.streamBuffer[this.workIndex + 1] = (byte)(intValue >> 16);
    this.streamBuffer[this.workIndex + 2] = (byte)(intValue >> 8);
    this.streamBuffer[this.workIndex + 3] = (byte)(intValue >> 0);
    this.workIndex += 4;
  }

  public void copyToByteArray(byte[] dst) {
    int copylen = this.getLength();
    if(copylen > dst.length) {
      copylen = dst.length;
    }

    System.arraycopy(this.streamBuffer, 0, dst, 0, copylen);
  }

  public byte[] getUnreadBytes() {
    int copylen = this.getLength() - this.workIndex;
    if(copylen > 0) {
      byte[] rval = new byte[copylen];
      System.arraycopy(this.streamBuffer, this.workIndex, rval, 0, copylen);
      return rval;
    } else {
      return null;
    }
  }

  public byte[] getCopyOfByteArray() {
    int copylen = this.getLength();
    byte[] rval = new byte[copylen];
    System.arraycopy(this.streamBuffer, 0, rval, 0, copylen);
    return rval;
  }

  public void writeInt(int intValue) {
    super.writeInt(intValue);
    this.writeIntToByteArray(intValue);
  }

  public void writeBoolean(boolean boolValue) {
    super.writeBoolean(boolValue);
    if(this.booleanWorkIndex == 0) {
      this.ensureCapacity(1);
      this.streamBuffer[this.workIndex++] = 0;
    }

    if(boolValue) {
      byte cur = this.streamBuffer[this.workIndex - 1];
      cur = (byte)(cur | 1 << this.booleanWorkIndex);
      this.streamBuffer[this.workIndex - 1] = cur;
    }

    ++this.booleanWorkIndex;
    this.booleanWorkIndex &= 7;
  }

  public void writeByte(byte byteValue) {
    super.writeByte(byteValue);
    this.booleanWorkIndex = 0;
    this.ensureCapacity(1);
    this.streamBuffer[this.workIndex++] = byteValue;
  }

  public void writeShort(int shortValue) {
    super.writeShort(shortValue);
    this.booleanWorkIndex = 0;
    this.ensureCapacity(2);
    this.streamBuffer[this.workIndex++] = (byte)(shortValue >> 8);
    this.streamBuffer[this.workIndex++] = (byte)(shortValue >> 0);
  }

  public void writeBytes(byte[] bytesValue) {
    super.writeBytes(bytesValue);
    if(bytesValue != null) {
      this.ensureCapacity(bytesValue.length + 4);
      this.writeIntToByteArray(bytesValue.length);
      System.arraycopy(bytesValue, 0, this.streamBuffer, this.workIndex, bytesValue.length);
      this.workIndex += bytesValue.length;
    } else {
      this.writeIntToByteArray(-1);
    }

  }

  public void writeBytes(byte[] bytesValue, boolean lenExclude) {
    if(!lenExclude) {
      this.writeBytes(bytesValue);
    } else if(bytesValue != null) {
      this.ensureCapacity(bytesValue.length);
      System.arraycopy(bytesValue, 0, this.streamBuffer, this.workIndex, bytesValue.length);
      this.workIndex += bytesValue.length;
    }

  }

  public void writeString(String s) {
    super.writeString(s);
    if(s != null) {
      try {
        byte[] e = s.getBytes("UTF-8");
        this.ensureCapacity(e.length + 4);
        this.writeIntToByteArray(e.length);
        if(e.length > 0) {
          System.arraycopy(e, 0, this.streamBuffer, this.workIndex, e.length);
          this.workIndex += e.length;
        }
      } catch (UnsupportedEncodingException var3) {
        var3.printStackTrace();
      }
    } else {
      this.writeIntToByteArray(-1);
    }

  }

  public void writeStringReference(String s) {
    super.writeStringReference(s);

    try {
      byte[] e = s.getBytes("UTF-8");
      this.ensureCapacity(e.length + 4);
      this.writeIntToByteArray(e.length);
      if(e.length > 0) {
        System.arraycopy(e, 0, this.streamBuffer, this.workIndex, e.length);
        this.workIndex += e.length;
      }
    } catch (UnsupportedEncodingException var3) {
      var3.printStackTrace();
    }

  }

  public String toString() {
    return this.readEndIndex > 0?StringUtils.dumpBytes(this.streamBuffer, this.workIndex, this.getLength() - this.workIndex):StringUtils.dumpBytes(this.streamBuffer, 0, this.getLength());
  }
}
